﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace PeisongWeb.buyer
{
    public partial class buyer_range : buyerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("AC", 1))
                {
                    rootCategoryBind();
                    subCategoryBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "buyer_index.html");
                    return;
                }
            }
        }

        private void rootCategoryBind()
        {
            DataTable dt = logic.buyerRange.rootCategorylistWithCompanyRange(logic.customer.companyId);
            ddl_CategoryRoot.DataSource = dt;
            ddl_CategoryRoot.DataBind();
            ddl_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        private void subCategoryBind()
        {
            //当大类不等于空的时候，就绑定小类
            if (ddl_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.buyerRange.subCategory_listWithCompanyRange(logic.customer.companyId, Convert.ToInt32(ddl_CategoryRoot.SelectedValue));
                ddl_CategorySub.DataSource = dt;
                ddl_CategorySub.DataBind();
            }
            else
            {
                ddl_CategorySub.Items.Clear();
                ddl_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
        }
        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            where.AppendFormat(" and buyerId={0}", logic.customer.companyId);
            if (!string.IsNullOrEmpty(ddl_CategorySub.SelectedValue))
                where.AppendFormat(" and categoryId={0}", ddl_CategorySub.SelectedValue);
            if (!string.IsNullOrEmpty(txt_ProductName.Text))
                where.AppendFormat(" and [name] like '%{0}%'", txt_ProductName.Text);
            if (!string.IsNullOrEmpty(txt_ProductNo.Text))
                where.AppendFormat(" and productNo='{0}'", txt_ProductNo.Text);

            DataSet ds = logic.buyerRange.list(where.ToString(), "productId,businessNature", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }
        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            subCategoryBind();
        }
        protected void lbn_search_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }

        protected void lbn_reset_Click(object sender, EventArgs e)
        {
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ReamrksEdit")
            {
                int productId = 0;
                string[] allitems = e.CommandArgument.ToString().Split(',');
                productId = Convert.ToInt32(allitems[0]);
                string businessnature = allitems[1];
                TextBox txt_remarks = (TextBox)e.Item.FindControl("txt_Remarks");
                TextBox txt_amount = (TextBox)e.Item.FindControl("txt_amount");
                string remarks = txt_remarks.Text;
                double amount = Convert.ToDouble( txt_amount.Text.ToString());
                logic.buyerRange.buyerRangeUpdateByRemark(logic.customer.companyId, productId, remarks,amount,businessnature);

                rptBind();
            }
        }
        public string BusinessNature(string businessnature)
        {
            if (businessnature == "1")
                return "工厂食堂级";
            else if (businessnature == "2")
                return "机关事业单位级";
            else if (businessnature == "3")
                return "院校级";
            else
                return "酒店餐饮级";
 
        }
    }
}
